package basic.courseLearn06.probelm;

import basic.courseLearn06.TreeNode;
import basic.courseLearn06.Utils;
import org.junit.Test;

public class FullBinaryTree {

    @Test
    public void test() {
        TreeNode root = new TreeNode(1);

        root.left = new TreeNode(2);
        root.right = new TreeNode(3);

        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
//        root.right.right = new TreeNode(7);


        new Utils().treePrint(root);

        System.out.println(isValidFullTree(root));
    }

    public boolean isValidFullTree(TreeNode root) {
        return solution_1(root);
    }


    /**
     * 解法1：递归解决：先求解最大深度，在求解是否为满二叉树
     * @param root   根节点
     * @return true：存在，false：不存在
     */
    private boolean solution_1(TreeNode root) {
        int depth = maxDepth(root);
        int nodes = maxNodes(root);
        System.out.println("depth: " + depth);
        System.out.println("nodes: " + nodes);
        //判断是否为满二叉树
        return ((1<<depth)-1)==nodes;
    }

    /**
     * 递归：求解最大深度
     * @param root
     * @return 最大深度
     */
    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth, rightDepth) + 1;
    }

    /**
     * 递归：求解最大节点数
     * @param root
     * @return 最大节点数
     */
    private int maxNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftNums = maxNodes(root.left);
        int rightNums = maxNodes(root.right);
        return leftNums+rightNums + 1;
    }



}
